argss <- commandArgs(trailingOnly = TRUE)

print(argss)

name <- argss[2]

names=read.table(name, col.names=c("file", "legend", "win", "sample"), stringsAsFactors=F)
print(names)

names=names[order(names$win),]
# names$legend=strsplit(names$file, '[.]')[[1]][2] #[.]


# rc92=read.table('92.rc.txt')
# rc91=read.table('CHG002591/1.sort.200000.sample.txt')
# rc93=sort(rc93)
# len=length(rc93)
# x=seq(from=0, to=1, length.out=len)

# rc93=rc93[length(which(rc93 == 0),]
# rc93=rc93[length(which(rc93 == 0):len),]
# rc93=rc93[length(which(rc93 == 0)):len,]
# len=length(rc93)

# len0=length(which(rc93 == 0))

# rc93=rc93[len0:len,]

# len=len-len0+1


# for (i in 1:100) {
	# rc93$V3[i]=sum(rc93[1:i])
	# rc93$V3= rc93$V3 / rc93$V3[100]
# }

# rc93$V4= rc93$V3 / rc93$V3[100]
# x=seq(0.01, 1, 0.01)


xlim=c(0, 1)
ylim=xlim
hlineby=.1
hline=seq((ylim[1]+hlineby), (ylim[2]-hlineby), hlineby)
vline=hline


pdf("lorenz.pdf", width=9, height=9)

par(ann=F, xaxs='i', yaxs='i')

plot(NA, type='n', xlim=xlim, ylim=ylim, cex.axis=.5)

abline(0,1, col='black')

abline(v=vline, h=hline, lty=3, col='gray')

title_main=paste("Lorenz curves of sample ", names$sample[1])

title(main=title_main, xlab="Cumulative fraction of genome", ylab="Cumulative fraction of total reads")

names$color=rainbow(nrow(names))

max_num=10000

# for (i in 1:nrow(names)) {
	# rc0=read.table(names[i,1])
	# rc=sample(rc0, )
	# rc=sort(rc)
	# len=length(rc)
	# sum=sum(rc)
	# for (i in 1:len) {
		# rc$V3[i]=sum(rc[1:i]) / sum
	# }
	# x=seq(from=0, to=1, length.out=len)
	# lines(x, rc$V3, col=$names$color[i])
	
	# names$legend[i]=strsplit(names$file[i], '[.]')[[1]][2]

# }

simu=runif(max_num)
num_sub=round(max_num/10)
count=hist(simu, breaks=num_sub, plot=F)$counts
rc=sort(count)
rc=c(0, rc)
num_sub=num_sub+1 # start from (0,0)
x=seq(from=0, to=1, length.out=num_sub)
sum=sum(rc)
rc_norm=NULL
for (ii in 1:num_sub) {
	rc_norm[ii]=sum(rc[1:ii]) / sum
}

gini_simu=1-((2*sum(rc_norm)-rc_norm[1]-rc_norm[num_sub])/(num_sub-1))
gini_simu_str=paste('simu', sprintf("%.4f", gini_simu), sep=' ')

lines(x, rc_norm, col='gray')

gini_str=vector('character')

for (i in 1:nrow(names)) {
	rc0=read.table(names[i,1])
	if (max_num < nrow(rc0)) {
		rc=sample(rc0$V1, max_num)
		len=max_num
	}else {
		rc=rc0$V1
		len=length(rc)
	}
	
	rc=sort(rc)
	
	sum=sum(rc)
	rc_norm=NULL
	for (ii in 1:len) {
		rc_norm[ii]=sum(rc[1:ii]) / sum
	}
	x=seq(from=0, to=1, length.out=len)
	lines(x, rc_norm, col=names$color[i])
	
	gini=1-((2*sum(rc_norm)-rc_norm[1]-rc_norm[len])/(len-1))
	gini_str[i]=paste(names$legend[i], sprintf("%.4f", gini), sep=' ')
	
}

legend("topleft", inset=.05, lty=c(1,1,1), c(gini_str, gini_simu_str), col=c(names$color, 'gray'))

# legend("topleft", inset=.05, lty=c(1,1,1), c(names$legend, 'simu'), col=c(names$color, 'gray'))

dev.off()

# lines(x, rc92$V4[len0:len], col='green') # bulk

# lines(x, rc91$V4[len0:len], col='blue') # bulk

# arrows(cd92[1], 0.08, x1 = cd92[1], y1 = cd92[1], code = 2, col = 'blue', arr.type="triangle")

# arrows(cd93[1], 0.08, x1 = cd93[1], y1 = cd93[1], code = 2, col = 'red')



# legend("topleft", inset=.05, lty=c(1,1,1), c("Bulk s93", "MALBAC s92", "MALBAC s91"), col=c("red", "green", "blue"))

